﻿package com.tudou.ad
{
    import com.tudou.ad.events.*;
    import com.tudou.newPlayer.*;
    import flash.events.*;
    import flash.utils.*;

    public class AdVideoBase extends AdDisplayBase
    {
        protected var _videoUrls:Array;
        protected var _effectiveTime:Number;
        protected var adVideoPlayer:TudouAdPlayer;
        protected var _loadComplete:Boolean;
        public var bufferMaxTimes:int = 5;
        protected var _requestComplete:Boolean;
        protected var _hasSendBuffer:Boolean;
        protected var _bufferFulled:Boolean = false;
        protected var bufferCount:int = 0;
        protected var timerTime:int = 1000;
        protected var _hasNearEnd:Boolean;
        protected var timer:Timer;
        protected var _adDurationType:Number;
        protected var _volume:Number = 0.5;
        protected var _videoUrl:String;
        protected var _playheadTime:Number = 0;
        public var timeout:int = 4;

        public function AdVideoBase(param1:uint, param2:uint)
        {
            super(param1, param2);
            return;
        }// end function

        public function get playheadTime() : Number
        {
            return _playheadTime;
        }// end function

        protected function videoEffectiveShow() : void
        {
            return;
        }// end function

        protected function bufferHlr() : void
        {
            var _loc_2:* = bufferCount + 1;
            bufferCount = _loc_2;
            if (bufferMaxTimes > 0 && bufferCount > bufferMaxTimes)
            {
                videoBuffer5Skip();
                _clearAd(true);
            }
            if (bufferCount > 1)
            {
                if (!_hasSendBuffer)
                {
                    _hasSendBuffer = true;
                    dispatchEvent(new AdVideoEvent(AdVideoEvent.BUFFER));
                }
                videoBuffer();
            }
            return;
        }// end function

        protected function _clearAd(param1:Boolean) : void
        {
            if (!_isShowing)
            {
                if (_isInited)
                {
                    clearComps();
                    adShowEnd();
                }
                return;
            }
            _isShowing = false;
            isSkip = param1;
            videoPlayheadtime();
            if (adVideoPlayer)
            {
                adVideoPlayer.volume = 0;
            }
            clearComps();
            adShowEnd();
            return;
        }// end function

        protected function videoNearEnd() : void
        {
            if (!_hasNearEnd)
            {
                _hasNearEnd = true;
                dispatchEvent(new AdVideoEvent(AdVideoEvent.SHOW_NEAR_END));
            }
            return;
        }// end function

        protected function removeListeners() : void
        {
            if (timer)
            {
                timer.stop();
                timer.removeEventListener(TimerEvent.TIMER, timerEventHandler);
            }
            if (adVideoPlayer)
            {
                adVideoPlayer.removeEventListener(MouseEvent.CLICK, clickHlr);
                adVideoPlayer.removeEventListener(PlayerEvent.CHANGE_VIDEO_STATUS, changeVideoStatusHlr);
                adVideoPlayer.removeEventListener(PlayerEvent.UPDATE, updateHlr);
            }
            return;
        }// end function

        private function checkVideoUrl() : Boolean
        {
            _videoUrl = _videoUrls.shift();
            trace("checkVideoUrl : " + _videoUrls);
            adItemInfo.videoUrl = _videoUrl;
            if (!_videoUrl)
            {
                return false;
            }
            return true;
        }// end function

        protected function completedHlr() : void
        {
            videoPlayComplete();
            _clearAd(false);
            return;
        }// end function

        override public function startAd() : void
        {
            _startAd();
            return;
        }// end function

        protected function videoPlayComplete() : void
        {
            return;
        }// end function

        protected function videoRequestStart() : void
        {
            return;
        }// end function

        public function set volume(param1:Number) : void
        {
            _volume = param1;
            if (adVideoPlayer)
            {
                adVideoPlayer.volume = _volume;
            }
            return;
        }// end function

        protected function timerEventHandler(event:TimerEvent) : void
        {
            if (timeout > 0 && timer.currentCount >= timeout && adVideoPlayer.loadSize <= 0)
            {
                videoRequestTimeout();
                _clearAd(true);
                return;
            }
            return;
        }// end function

        protected function videoSubRequestError() : void
        {
            return;
        }// end function

        protected function changeVideoStatusHlr(event:PlayerEvent) : void
        {
            switch(adVideoPlayer.videoStatus.status)
            {
                case VideoStatus.ERROR:
                {
                    errorHlr();
                    break;
                }
                case VideoStatus.BUFFERING:
                {
                    bufferHlr();
                    break;
                }
                case VideoStatus.PLAYING:
                {
                    break;
                }
                case VideoStatus.READY:
                {
                    bufferFullHlr();
                    break;
                }
                case VideoStatus.COMPLETED:
                {
                    _playheadTime = adVideoPlayer.totalTime;
                    completedHlr();
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            return;
        }// end function

        protected function createListeners() : void
        {
            if (timer)
            {
                timer.addEventListener(TimerEvent.TIMER, timerEventHandler);
            }
            if (adVideoPlayer)
            {
                adVideoPlayer.addEventListener(MouseEvent.CLICK, clickHlr);
                adVideoPlayer.addEventListener(PlayerEvent.CHANGE_VIDEO_STATUS, changeVideoStatusHlr);
                adVideoPlayer.addEventListener(PlayerEvent.UPDATE, updateHlr);
            }
            return;
        }// end function

        protected function videoUrlNull() : void
        {
            return;
        }// end function

        override public function initToShow() : void
        {
            if (!adItemInfo || _isInited)
            {
                return;
            }
            _isInited = true;
            _videoUrls = adItemInfo.videoUrl.split(",");
            checkVideoUrl();
            _adDurationType = adItemInfo.durationType;
            if (!isNaN(adItemInfo.durationEffective) && adItemInfo.durationEffective > 0)
            {
                _effectiveTime = adItemInfo.durationEffective;
            }
            else
            {
                _effectiveTime = NaN;
            }
            adVideoPlayer = new TudouAdPlayer(mainWidth, mainHeight);
            adVideoPlayer.width = mainWidth;
            adVideoPlayer.height = mainHeight;
            adVideoPlayer.setBackground(0, 1);
            addChild(adVideoPlayer);
            timer = new Timer(timerTime);
            createListeners();
            bufferCount = 0;
            _isSkip = false;
            _bufferFulled = false;
            _requestComplete = false;
            _loadComplete = false;
            _hasNearEnd = false;
            _hasSendBuffer = false;
            return;
        }// end function

        protected function errorHlr() : void
        {
            videoSubRequestError();
            if (checkVideoUrl())
            {
                try
                {
                    if (adVideoPlayer)
                    {
                        adVideoPlayer.close();
                    }
                }
                catch (error:Error)
                {
                }
                newPlay();
            }
            else
            {
                videoRequestError();
                _clearAd(true);
            }
            return;
        }// end function

        protected function videoRequestTimeout() : void
        {
            return;
        }// end function

        protected function videoRequestError() : void
        {
            return;
        }// end function

        protected function clickHlr(event:Event) : void
        {
            dispatchEvent(new AdVideoEvent(AdVideoEvent.CLICK));
            return;
        }// end function

        override public function clearAd() : void
        {
            _clearAd(true);
            return;
        }// end function

        protected function videoBuffer5Skip() : void
        {
            return;
        }// end function

        protected function updateHlr(event:PlayerEvent) : void
        {
            dispatchEvent(new AdVideoEvent(AdVideoEvent.TIME));
            if (adVideoPlayer.playTime >= _effectiveTime)
            {
                _effectiveTime = NaN;
                videoEffectiveShow();
            }
            if (adVideoPlayer.playTime >= _playheadTime)
            {
                _playheadTime = adVideoPlayer.playTime;
            }
            if (_adDurationType > 0 && adVideoPlayer.playTime >= _adDurationType - 3)
            {
                videoNearEnd();
            }
            if (adVideoPlayer.loadSize > 0 && !_requestComplete)
            {
                _requestComplete = true;
                videoRequestComplete();
            }
            if (!_loadComplete && adVideoPlayer.loadSize >= adVideoPlayer.totalSize && adVideoPlayer.videoStatus.status >= VideoStatus.READY)
            {
                _loadComplete = true;
                videoLoadComplete();
                videoNearEnd();
            }
            if (_adDurationType > 0 && adVideoPlayer.playTime >= _adDurationType)
            {
                completedHlr();
            }
            return;
        }// end function

        protected function _startAd() : void
        {
            if (_isShowing)
            {
                return;
            }
            _isShowing = true;
            if (_videoUrl == null || _videoUrl == "")
            {
                videoUrlNull();
                _clearAd(true);
                return;
            }
            if (_adDurationType > 0)
            {
                timer.reset();
                timer.start();
            }
            videoRequestStart();
            newPlay();
            return;
        }// end function

        protected function videoLoadComplete() : void
        {
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            if (adVideoPlayer)
            {
                adVideoPlayer.setWidthHeight(param1, param2);
            }
            return;
        }// end function

        protected function videoRequestComplete() : void
        {
            return;
        }// end function

        protected function videoBuffer() : void
        {
            return;
        }// end function

        private function newPlay() : void
        {
            adVideoPlayer.volume = _volume;
            videoSubRequestStart();
            adVideoPlayer.load(_videoUrl);
            adVideoPlayer.play();
            return;
        }// end function

        protected function clearComps() : void
        {
            removeListeners();
            if (timer)
            {
                timer.stop();
            }
            try
            {
                if (adVideoPlayer)
                {
                    adVideoPlayer.close();
                }
            }
            catch (error:Error)
            {
            }
            while (this.numChildren > 0)
            {
                
                this.removeChildAt(0);
            }
            adVideoPlayer = null;
            timer = null;
            _adItemInfo = null;
            _videoUrl = null;
            _isShowing = false;
            _isInited = false;
            return;
        }// end function

        protected function videoSubRequestStart() : void
        {
            return;
        }// end function

        protected function adShowEnd() : void
        {
            dispatchEvent(new AdVideoEvent(AdVideoEvent.SHOW_END));
            return;
        }// end function

        protected function videoPlayheadtime() : void
        {
            return;
        }// end function

        protected function bufferFullHlr() : void
        {
            if (!_bufferFulled)
            {
                _bufferFulled = true;
                dispatchEvent(new AdVideoEvent(AdVideoEvent.SHOW_START));
            }
            return;
        }// end function

    }
}
